<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;

use wxpay\database\WxPayUnifiedOrder;
use wxpay\JsApiPay;
use wxpay\NativePay;
use wxpay\PayNotifyCallBack;
use think\Log;
use wxpay\WxPayApi;
use wxpay\WxPayConfig;




use alipay\wappay\buildermodel\AlipayTradeWapPayContentBuilder;
use alipay\wappay\service\AlipayTradeService;

use pinganpay\Webapp;


class Pay extends Controller
{


    /**
     * 微信支付
     * @return [type] [description]
     */
    public function wxpay($data)
    {


        if (!empty($data)) {
            //获取用户openid
            $tools = new JsApiPay();
            $openId = Db::name('userinfo')->where(array('uid'=>$data['uid']))->value('openid');

            if(!$openId){
                return WPreturn('openId不存在',-1);
            }
            //统一下单
            $input = new WxPayUnifiedOrder();
            $input->setBody("会员余额充值");
            $input->setAttach("web_user_pay_ing");

            $input->setOutTradeNo($data['balance_sn']);
            $input->setTotalFee($data['bpprice'] * 100);
            $input->setTimeStart(date("YmdHis"));
            $input->setTimeExpire(date("YmdHis", time() + 600));
            $input->setGoodsTag("goods");
            $input->setNotifyUrl("http://".$_SERVER['SERVER_NAME']."/index/wechat/notifyurl/bpid/".$data['bpid']);
            $input->setTradeType("JSAPI");
            $input->setOpenid($openId);

            $order = WxPayApi::unifiedOrder($input);

            $jsApiParameters = $tools->getJsApiParameters($order);

            /*
            $this->assign('order',$order);
            $this->assign('jsApiParameters',$jsApiParameters);
            return $this->fetch('jsapi');
            */
           return $jsApiParameters;
        }

    }

    /**
     * 中云支付
     * @return [type] [description]
     */
    public function zypay($data)
    {
        

    $pay_memberid = "12720";   //商户ID
    $pay_orderid = $data['balance_sn'];    //订单号
    $pay_amount = $data['bpprice'];    //交易金额
    $pay_applydate = date("Y-m-d H:i:s");  //订单时间
    $pay_bankcode = "WftZfb";   //银行编码
    $pay_notifyurl = "http://".$_SERVER['SERVER_NAME']."/index/pay/zypay_notify.html";   //服务端返回地址
    $pay_callbackurl = "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";  //页面跳转返回地址
    
    $Md5key = "y9tHLB3aL39N6dBGx9gIqvQ6d55z1D";   //密钥
    
    $tjurl = "http://zy.cnzypay.com/Pay_Index.html";   //提交地址,如有变动请到官网下载最新接口文档
    
    $requestarray = array(
            "pay_memberid" => $pay_memberid,
            "pay_orderid" => $pay_orderid,
            "pay_amount" => $pay_amount,
            "pay_applydate" => $pay_applydate,
            "pay_bankcode" => 'WXZF',
            "pay_notifyurl" => $pay_notifyurl,
            "pay_callbackurl" => $pay_callbackurl
			
        );
        
        ksort($requestarray);
        reset($requestarray);
        $md5str = "";
        foreach ($requestarray as $key => $val) {
            $md5str = $md5str . $key . "=>" . $val . "&";
        }
        
        $sign = strtoupper(md5($md5str . "key=" . $Md5key)); 
        $requestarray["pay_md5sign"] = $sign;
		$requestarray["tongdao"]="JiuXiaoWxSm";
		
		
		
		//初始化
		$curl = curl_init();
		//设置抓取的url
		curl_setopt($curl, CURLOPT_URL, $tjurl);
		//设置头文件的信息作为数据流输出
		curl_setopt($curl, CURLOPT_HEADER, 1);
		//设置获取的信息以文件流的形式返回，而不是直接输出。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//设置post方式提交
		curl_setopt($curl, CURLOPT_POST, 1);
		//设置post数据
		$post_data = array(
			"username" => "coder",
			"password" => "12345"
			);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $requestarray);
		//执行命令
		$data = curl_exec($curl);
		//关闭URL请求
		curl_close($curl);
		//显示获得的数据
		$img = '';
		
		$res = explode('<img src="',$data);
		
		if(count($res) == 2){
			$res2 = explode('"',$res[1]);
			
			if(isset($res2['0'])){
				$img = 'http://zy.cnzypay.com/'.$res2['0'];
			}else{
				return WPreturn(lang('systemError'),-1);
			}
		}else{
			return WPreturn(lang('systemError'),-1);
		}
		
		return $img;
		
		exit;
		
        
        $str = '<form id="Form1" name="Form1" method="post" action="' . $tjurl . '">';
        foreach ($requestarray as $key => $val) {
            $str = $str . '<input type="hidden" name="' . $key . '" value="' . $val . '">';
        }
        $str = $str . '<input type="submit" value="'.lang('submission').'">';
        $str = $str . '</form>';
        $str = $str . '<script>';
        $str = $str . 'document.Form1.submit();';
        $str = $str . '</script>';
        
        return $str;
        

    }


    public function zypay_notify()
    {
        
        $ReturnArray = array( // 返回字段
            "memberid" => $_REQUEST["memberid"], // 商户ID
            "orderid" =>  $_REQUEST["orderid"], // 订单号
            "amount" =>  $_REQUEST["amount"], // 交易金额
            "datetime" =>  $_REQUEST["datetime"], // 交易时间
            "returncode" => $_REQUEST["returncode"]
        );
      
        $Md5key = "y9tHLB3aL39N6dBGx9gIqvQ6d55z1D";
        //$sign = $this->md5sign($Md5key, $ReturnArray);
        
        ///////////////////////////////////////////////////////
        ksort($ReturnArray);
        reset($ReturnArray);
        $md5str = "";
        foreach ($ReturnArray as $key => $val) {
            $md5str = $md5str . $key . "=>" . $val . "&";
        }
        $sign = strtoupper(md5($md5str . "key=" . $Md5key)); 
        ///////////////////////////////////////////////////////
        if ($sign == $_REQUEST["sign"]) {
            if ($_REQUEST["returncode"] == "00") {
                   $this->notify_ok_dopay($ReturnArray['orderid'],$ReturnArray['amount']);
                   exit("ok");
            }
        }

        cache('');
    }




    public function qianbaotong($data,$pay_type)
    {
        
        
        /*
        * 商户id，由平台分配
        */
        $parter = 1743;
        
        /*
        * 准备使用网银支付的银行
        */
        $type = $pay_type;
        
        /*
        * 支付金额
        */
        $value = $data['bpprice'];
        
        /*
        * 请求发起方自己的订单号，该订单号将作为平台的返回数据
        */
        $orderid = $data['balance_sn'];
        
        /*
        * 在下行过程中返回结果的地址，需要以http://开头。
        */
        $callbackurl = "http://".$_SERVER['SERVER_NAME']."/index/pay/qdb_notify.html";;
        
        /*
        * 支付完成之后平台会自动跳转回到的页面
        */
        $hrefbackurl = "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";;
        
        /*
        * 商户密钥
        */
        $key = '62cccea8b4c3410aa4b0fac9f0020259';
        

        $shidai_bank_url   = 'http://gateway.qpabc.com/bank/index.aspx';

        $url = "parter=". $parter ."&type=". $type ."&value=". $value. "&orderid=". $orderid ."&callbackurl=". $callbackurl;
        //签名
        $sign   = md5($url. $key);
        
        //最终url
        $url    = $shidai_bank_url . "?" . $url . "&sign=" .$sign. "&hrefbackurl=". $hrefbackurl;

        if(in_array($pay_type,array(1005,1006,1007))){
            return $url;
        }else{
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 500);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_URL, $url);
         
            $res = curl_exec($curl);
            curl_close($curl);
            
            return $res;
        }
        

    }





    public function qdb_notify()
    {
        cache('qdb_test',$_GET);
        //$_GET = cache('qdb_test');
        //获取返回的下行数据
        
        //$sysorderid     = trim($_GET['sysorderid']);
        //$completiontime     = trim($_GET['systime']);

        //进行爱扬签名认证
        $key = '62cccea8b4c3410aa4b0fac9f0020259';
        header('Content-Type:text/html;charset=GB2312');
        $orderid        = trim($_GET['orderid']);
        $opstate        = trim($_GET['opstate']);
        $ovalue         = trim($_GET['ovalue']);
        $sign           = trim($_GET['sign']);
        
        //订单号为必须接收的参数，若没有该参数，则返回错误
        if(empty($orderid)){
            die("opstate=-1");      //签名不正确，则按照协议返回数据
        }
        
        $sign_text  = "orderid=$orderid&opstate=$opstate&ovalue=$ovalue".$key;
        $sign_md5 = md5($sign_text);
        if($sign_md5 != $sign){
            die("opstate=-2");      //签名不正确，则按照协议返回数据
        }
        $this->notify_ok_dopay($orderid,$ovalue);
        die("opstate=0");       

    }

    
    public function alipay($data){

        $config = array (   
        //应用ID,您的APPID。
        'app_id' => "2017022705923867",

        //商户私钥，您的原始格式RSA私钥
        'merchant_private_key' => "MIIEowIBAAKCAQEA4SvhwaggPK6YcT9KFcWatlWzmPOGuinPibsSuQOKOzIdndmsobx8gxYsL40SBJZJ7gUzLW53WUPJiu1Cn2K6b1m/PsOQNl6WRQD7fD62fCO5z3Wqitx9bts/LoUbX7vb4Dxpplw7KKVikUCBwe75hOTuhAfQ7dqGzbE0xfKjO2ugRBDceCy5InBK/xfvVbNRk+1DZyexLSUJx7pm5nUCkVj81URlnQYzcW06OBjvSSecTpmAktbvruZE450vhxkfDzxp47R0qba4c8ALRrDlnrUb29EPD4TFmXWGxteZQBQWKbEJWte7tV/sGW9ed/6QeC8A9N3CalnzXpqIF4hpcQIDAQABAoIBAFOUPDnrs/uSOxdeDJvEO0cOzJkrW4jiWByhibOO8tJCKegbkg5+riDiLAiCbnuxZUOqPnLQnBBQLxEYPDB5LwaB45DiejcUKOb4FGDrzkSJ5kBxRppAeXaafvs/gQep7VVwVy7e8T6HFO0haoiXsZp4d2gelpiTEpJrAlGvXJODDzMJPoEcpeHEDUUroH1+PXCGmZL8mB5a+ZzcP14IRsxWEygTy64MADa5RQ3U7qpSKSSiCRvTp1CUIMTEzgcYDziWCpWwdDEjrmyoQy3sUpdxwFrShQ0gwxgFgfawlR31d1rJxarF1/ZOsEa3RbbDdJWS4MwgMbYi70gB4UFTDLkCgYEA9bsfRblnK44C0oWTVxemxtuP96JPpqFj+jtcUMSBDZvnXyV5TKMWiP+agefWgQ5Gz5z6yBEicXvMcC9qcYf2nNnZYeTiCJmSof8dqWg5Uah3l+GBBJ13AVcrhJv/pm1Gkm3+WubREQBEXq3l9F/cRyEMzF2XWFCdrjX7R1JufssCgYEA6pTPTrIxufboxtJJXusdSSueqxN5see4TiqKXizRMuUaEF9h0iHd1fvxHWSMo3zlLt8s4LrR3PlKGXo88RnScNvRE3KyvznLkRhwdaFvTQjSUrMe+wV+OIRJm9UnV2ysqrB3w8+GP6iZPdiRN/AW4rkoPf0SMo2IGYR1/JsLlTMCgYBJxXqW+RlDFyg7wYRBYkVcb/AhvOXCtbMJHacSTFweFM76Xoqy+kc6q9nb5Bket4WEsLENPS+k+DChAWsoWFQuNKyxWgCN6mT+I1PpVvPWUwhMXZPZKdjfWycicZ7nfOjx7vmsmpzrSLQ95GEj41+DLyXjeLmF9vXPpj8g41tuzwKBgQCK31nzDs8ddqzLt4Y0KSCHRsmCId9zkOitbcXIhuO6K6NIeg8hJWd83NAbRIF17+SF4R1iVXcUSIizmIgne8/3fErEJqznREHdPgilutJ3WneY+e2nUdMthjNFi+TkfrOhwSLFyz+AxEEkOeeOpBYIVvEZ8Y4qW1ttL9vhlbA/vQKBgGrC0rchgbdL9Ehd8lG5yDYce1N2ZAxDLLGzyxbp76OExGQMj6vBJZeGp1S6ICNLSbbVWD3Wflk1d0o1o47GgF9p+PXJyeKes2ZTOByH0R+8M92fjVXmOxNUvC0oiqVTlFLd18cH4Yd9d6DaA+msmnkJY62tyZgceJcAmTwRVHkJ",
        
        //异步通知地址
        'notify_url' => "http://".$_SERVER['SERVER_NAME']."/index/pay/alipay_notify.html",
        
        //同步跳转
        'return_url' => "http://".$_SERVER['SERVER_NAME']."/index/user/index.html",

        //编码格式
        'charset' => "UTF-8",

        //签名方式
        'sign_type'=>"RSA2",

        //支付宝网关
        'gatewayUrl' => "https://openapi.alipay.com/gateway.do",

        //支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
        'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4SvhwaggPK6YcT9KFcWatlWzmPOGuinPibsSuQOKOzIdndmsobx8gxYsL40SBJZJ7gUzLW53WUPJiu1Cn2K6b1m/PsOQNl6WRQD7fD62fCO5z3Wqitx9bts/LoUbX7vb4Dxpplw7KKVikUCBwe75hOTuhAfQ7dqGzbE0xfKjO2ugRBDceCy5InBK/xfvVbNRk+1DZyexLSUJx7pm5nUCkVj81URlnQYzcW06OBjvSSecTpmAktbvruZE450vhxkfDzxp47R0qba4c8ALRrDlnrUb29EPD4TFmXWGxteZQBQWKbEJWte7tV/sGW9ed/6QeC8A9N3CalnzXpqIF4hpcQIDAQAB",
        
    
);


        //商户订单号，商户网站订单系统中唯一订单号，必填
        $out_trade_no = $data['balance_sn'];

        //订单名称，必填
        $subject = '用户充值';

        //付款金额，必填
        $total_amount = $data['bpprice'];

        //商品描述，可空
        $body = '';

        //超时时间
        $timeout_express="1m";

        $payRequestBuilder = new AlipayTradeWapPayContentBuilder();
        $payRequestBuilder->setBody($body);
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setOutTradeNo($out_trade_no);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setTimeExpress($timeout_express);
        
        $payResponse = new AlipayTradeService($config);

        $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);

        return $result;

    }


    /**
     * izpay
     * @author lukui  2017-08-16
     * @return [type] [description]
     */
    public function izpay_wx($data)
    {
        
        header("Access-Control-Allow-Origin: *");

        $url = 'http://www.izpay.cn:9002/thirdsync_server/third_pay_server';
        
        $para['out_trade_no'] = $data['balance_sn'];
        $para['mer_id'] = 'pay177';
        $para['goods_name'] = 'userpay';
        $para['total_fee'] = $data['bpprice']*100;
        $para['callback_url'] =  "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";
        $para['notify_url'] = "http://".$_SERVER['SERVER_NAME']."/index/pay/izpay_wx_notify.html";
       
        $para['attach'] =  '709';
        $para['nonce_str'] = mt_rand(time(),time()+rand());
        $para['pay_type'] = '003';
        $key = "c71elu2cq25b5m8ks99fxhqteljugo6m";
        
        
        
        
        $sign_str = 'mer_id='.$para['mer_id'].'&nonce_str='.$para['nonce_str'].'&out_trade_no='.$para['out_trade_no'].'&total_fee='.$para['total_fee'].'&key='.$key;
        
        //echo $sign_str;
        
        $para['sign'] = md5($sign_str); 
        
        
        $str = "";
        foreach($para as $key=>$val){
        $str .= $key.'='.$val.'&';
        }
        $newstr = substr($str,0,strlen($str)-1); 
        
        $pay_url = $url.'?'.$newstr;
        
        
        $temp_data = file_get_contents($pay_url);
        $result = json_decode($temp_data,true);
        
        
        return $temp_data;
    }

    public function izpay_alipay($data)
    {
        
        header("Access-Control-Allow-Origin: *");

        $url = 'http://www.izpay.cn:9002/thirdsync_server/third_pay_server';
        
        $para['out_trade_no'] = $data['balance_sn'];
        $para['mer_id'] = 'pay177';
        $para['goods_name'] = 'userpay';
        $para['total_fee'] = $data['bpprice']*100;
        $para['callback_url'] =  "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";
        $para['notify_url'] = "http://".$_SERVER['SERVER_NAME']."/index/pay/izpay_wx_notify.html";
       
        $para['attach'] =  '709';
        $para['nonce_str'] = mt_rand(time(),time()+rand());
        $para['pay_type'] = '006';
        $key = "c71elu2cq25b5m8ks99fxhqteljugo6m";
        
        
        
        
        $sign_str = 'mer_id='.$para['mer_id'].'&nonce_str='.$para['nonce_str'].'&out_trade_no='.$para['out_trade_no'].'&total_fee='.$para['total_fee'].'&key='.$key;
        
        //echo $sign_str;
        
        $para['sign'] = md5($sign_str); 
        
        
        $str = "";
        foreach($para as $key=>$val){
        $str .= $key.'='.$val.'&';
        }
        $newstr = substr($str,0,strlen($str)-1); 
        
        $pay_url = $url.'?'.$newstr;
        
        
        $temp_data = file_get_contents($pay_url);
        $result = json_decode($temp_data,true);
        
        
        return $temp_data;
    }
    
    
    public function izpay_wx_notify(){
        $data = input('');
        if(!isset($data['out_trade_no'])){
            return false;
        }
        $this->notify_ok_dopay($data['out_trade_no'],$data['total_fee']/100);
        return true;
        
    }





    /**
     * 平安微信扫码支付
     * @author lukui  2017-08-17
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function pingan_code($balance,$code)
    {
        $papay = new Webapp();

        $data['out_no'] = $balance['balance_sn'];
        $data['pmt_tag'] = $code;
        $data['original_amount'] = $balance['bpprice']*100;
        $data['trade_amount'] = $balance['bpprice']*100;
        $data['ord_name'] = 'userpay';
        $data['auth_code'] = "";
        $data['jump_url'] = "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";;
        $data['notify_url'] = "http://".$_SERVER['SERVER_NAME']."/index/pay/panotify.html";;
        $result = $papay->api("payorder",$data);
        return $result;
    }

    public function panotify()
    {
        
        $data = $_REQUEST;

        if(isset($data['amount']) && isset($data['out_no'])){
            $this->notify_ok_dopay($data['out_no'],$data['amount']/100);
        }
        

    }
    
public function hf_codewx($data)
    {
		$returnurl = "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";;
        $notifyurl = "http://".$_SERVER['SERVER_NAME']."/index/pay/hf_codenotify.html";;
        $remark = lang('userRecharge');
        $customerid = '11243';
        $userkey = 'e6ba610aafd824879d4638f2839f3162570c6f96';
        $data['bpprice'] = sprintf("%.2f",$data['bpprice']);

        $sign_str = 'version=1.0&customerid='.$customerid.'&total_fee='.$data['bpprice'].'&sdorderno='.$data['balance_sn'].'&notifyurl='.$notifyurl.'&returnurl='.$returnurl.'&'.$userkey;

        $sign=md5($sign_str);
        $posthtml = <<<A
        <form name="pay" id="pay" action="http://www.7t1.cn/apisubmit" method="post">
            <input type="hidden" name="version" value="1.0">
            <input type="hidden" name="customerid" value="{$customerid}">
            <input type="hidden" name="sdorderno" value="{$data['balance_sn']}">
            <input type="hidden" name="total_fee" value="{$data['bpprice']}">
            <input type="hidden" name="paytype" value="gzhpay">
            <input type="hidden" name="notifyurl" value="$notifyurl">
            <input type="hidden" name="returnurl" value="$returnurl">
            <input type="hidden" name="remark" value="$remark">
            <input type="hidden" name="bankcode" value="">
            <input type="hidden" name="sign" value="$sign">
        </form>
		<script>
        	document.getElementById('pay').submit()
        </script>
A;

        return $posthtml;
    }
	public function hf_codezfb($data)
    {
		$returnurl = "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";;
        $notifyurl = "http://".$_SERVER['SERVER_NAME']."/index/pay/hf_codenotify.html";;
         $remark = lang('userRecharge');
        $customerid = '11243';
        $userkey = 'e6ba610aafd824879d4638f2839f3162570c6f96';
        $data['bpprice'] = sprintf("%.2f",$data['bpprice']);
        $sign_str = 'version=1.0&customerid='.$customerid.'&total_fee='.$data['bpprice'].'&sdorderno='.$data['balance_sn'].'&notifyurl='.$notifyurl.'&returnurl='.$returnurl.'&'.$userkey;
        $sign=md5($sign_str);
        $posthtml = <<<A

        <form name="pay" id="pay" action="http://www.7t1.cn/apisubmit" method="post">
            <input type="hidden" name="version" value="1.0">
            <input type="hidden" name="customerid" value="{$customerid}">
            <input type="hidden" name="sdorderno" value="{$data['balance_sn']}">
            <input type="hidden" name="total_fee" value="{$data['bpprice']}">
            <input type="hidden" name="paytype" value="alipay">
            <input type="hidden" name="notifyurl" value="$notifyurl">
            <input type="hidden" name="returnurl" value="$returnurl">
            <input type="hidden" name="remark" value="$remark">
            <input type="hidden" name="bankcode" value="">
            <input type="hidden" name="sign" value="$sign">
        </form>
		<script>
        	document.getElementById('pay').submit()
        </script>
A;

        return $posthtml;
    }
    public function hf_codenotify($data,$paytype,$bankcode)
    {
         $userkey='70741f94f13c818721dec361f80bec6f6ae923c1';
        $status=$_POST['status'];
        $customerid=$_POST['customerid'];
        $sdorderno=$_POST['sdorderno'];
        $total_fee=$_POST['total_fee'];
        $paytype=$_POST['paytype'];
        $sdpayno=$_POST['sdpayno'];
        $remark=$_POST['remark'];
        $server=$_POST['server'];
        $sign=$_POST['sign'];

        $mysign=md5('customerid='.$customerid.'&status='.$status.'&sdpayno='.$sdpayno.'&sdorderno='.$sdorderno.'&total_fee='.$total_fee.'&paytype='.$paytype.'&'.$userkey);

        if($sign==$mysign){
            if($status=='1'){

                $this->notify_ok_dopay($sdorderno,$total_fee);

                echo 'success';
            } else {
                echo 'fail';
            }
        } else {
            echo 'signerr';
        }
    }


    /**
     * 综合支付-锐捷支付-http://www.19fk.com/
     * @author lukui  2017-08-30
     * @return [type] [description]
     */
    public function zhpay($data,$paytype,$bankcode)
    {
        if($data || $paytype || $bankcode){
            WPreturn('参数错误！',-1);
        }


        $returnurl = "http://".$_SERVER['SERVER_NAME']."/index/user/index.html";;
        $notifyurl = "http://".$_SERVER['SERVER_NAME']."/index/pay/zhpaynotify.html";;

        $remark = lang('userRecharge');
        $customerid = '20081203';
        $userkey = '70741f94f13c818721dec361f80bec6f6ae923c1';
        $data['bpprice'] = sprintf("%.2f",$data['bpprice']);

        $sign_str = 'version=1.0&customerid='.$customerid.'&total_fee='.$data['bpprice'].'&sdorderno='.$data['balance_sn'].'&notifyurl='.$notifyurl.'&returnurl='.$returnurl.'&'.$userkey;

        $sign=md5($sign_str);
        

        $posthtml = <<<A

        <form name="pay" action="http://www.19fk.com/apisubmit" method="post">
            <input type="hidden" name="version" value="1.0">
            <input type="hidden" name="customerid" value="{$customerid}">
            <input type="hidden" name="sdorderno" value="{$data['balance_sn']}">
            <input type="hidden" name="total_fee" value="{$data['bpprice']}">
            <input type="hidden" name="paytype" value="{$paytype}">
            <input type="hidden" name="notifyurl" value="$notifyurl">
            <input type="hidden" name="returnurl" value="$returnurl">
            <input type="hidden" name="remark" value="$remark">
            <input type="hidden" name="bankcode" value="{$bankcode}">
            <input type="hidden" name="sign" value="$sign">
        </form>
A;

        return $posthtml;

    }

    public function zhpaynotify()
    {
        
        $userkey='70741f94f13c818721dec361f80bec6f6ae923c1';
        $status=$_POST['status'];
        $customerid=$_POST['customerid'];
        $sdorderno=$_POST['sdorderno'];
        $total_fee=$_POST['total_fee'];
        $paytype=$_POST['paytype'];
        $sdpayno=$_POST['sdpayno'];
        $remark=$_POST['remark'];
        $server=$_POST['server'];
        $sign=$_POST['sign'];

        $mysign=md5('customerid='.$customerid.'&status='.$status.'&sdpayno='.$sdpayno.'&sdorderno='.$sdorderno.'&total_fee='.$total_fee.'&paytype='.$paytype.'&'.$userkey);

        if($sign==$mysign){
            if($status=='1'){

                $this->notify_ok_dopay($sdorderno,$total_fee);

                echo 'success';
            } else {
                echo 'fail';
            }
        } else {
            echo 'signerr';
        }

    }

    








    public function notify_ok_dopay($order_no,$order_amount)
    {
        
        if(!$order_no || !$order_amount){
            
            return false;
        }

        $balance = db('balance')->where('balance_sn',$order_no)->find();
        if(!$balance){
            
            return false;
        }

        if($balance['bpprice'] != $order_amount){
            
            return false;
        }

        if($balance['bptype'] != 3){
            
            return true;
        }
        $_edit['bpid'] = $balance['bpid'];
        $_edit['bptype'] = 1;
        $_edit['isverified'] = 1;
        $_edit['cltime'] = time();
        $_edit['bpbalance'] = $balance['bpbalance']+$balance['bpprice'];
        
        $is_edit = db('balance')->update($_edit);
        
        if($is_edit){
            // add money
            $_ids=db('userinfo')->where('uid',$balance['uid'])->setInc('usermoney',$balance['bpprice']);
            if($_ids){
                //资金日志
                set_price_log($balance['uid'],1,$balance['bpprice'],lang('recharge'),lang('userRecharge'),$_edit['bpid'],$_edit['bpbalance']);
            }
            
            return true;
        }else{
            
            return false;
        }

    }


    public function test_not()
    {
        
        dump(cache('padata'));
    }


}


?>